package com.xli.organ.role.framerolecategory.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.TreeNode;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.component.model.TreeModel;
import com.xli.dto.component.util.TreeUtil;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.organ.role.framerolecategory.entity.FrameRoleCategory;
import com.xli.organ.role.framerolecategory.entity.dto.RoleCategoryAddDTO;
import com.xli.organ.role.framerolecategory.entity.dto.RoleCategorySearchDTO;
import com.xli.organ.role.framerolecategory.entity.dto.RoleCategoryUpdateDTO;
import com.xli.organ.role.framerolecategory.entity.mapper.IFrameRoleCategoryMapper;
import com.xli.organ.role.framerolecategory.entity.vo.RoleCategoryVO;
import com.xli.organ.role.framerolecategory.service.IFrameRoleCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "角色分类")
@RestController
@RequestMapping("/frameRoleCategory")
public class FrameRoleCategoryController extends BaseController {

    @Autowired
    private IFrameRoleCategoryService iFrameRoleCategoryService;

    @Operation(summary = "保存分类")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) RoleCategoryAddDTO dto) {
        FrameRoleCategory frameRoleCategory = iFrameRoleCategoryService.findByName(dto.getCategoryName());
        if (frameRoleCategory == null) {
            frameRoleCategory = IFrameRoleCategoryMapper.INSTANCE.toEntity(dto);
            frameRoleCategory.setId(IdUtil.getSnowflakeNextId());
            if (iFrameRoleCategoryService.insert(frameRoleCategory)) {
                return new ResultVO<>(Status.SUCCESS, "保存成功");
            }
            return new ResultVO<>(Status.FAILED, "保存失败");
        }
        return new ResultVO<>(Status.FAILED, "保存失败，分类名已存在");
    }

    @Operation(summary = "删除分类")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iFrameRoleCategoryService.delete(Long.valueOf(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新分类")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) RoleCategoryUpdateDTO dto) {
        FrameRoleCategory frameRoleCategory = iFrameRoleCategoryService.findByName(dto.getCategoryName());
        if (frameRoleCategory == null || dto.getId().equals(frameRoleCategory.getId().toString())) {
            if (iFrameRoleCategoryService.update(IFrameRoleCategoryMapper.INSTANCE.toEntity(dto))) {
                return new ResultVO<>(Status.SUCCESS, "更新成功");
            }
            return new ResultVO<>(Status.FAILED, "更新失败");
        }
        return new ResultVO<>(Status.FAILED, "更新失败，分类已存在");

    }

    @Operation(summary = "查询详情")
    @PostMapping(value = "/detail/{id}")
    public ResultVO<RoleCategoryVO> detail(@PathVariable("id") @NotBlank(message = "id必填") String id) {
        FrameRoleCategory frameRoleCategory = iFrameRoleCategoryService.find(Long.valueOf(id));
        if (frameRoleCategory != null) {
            return new ResultVO<>(Status.SUCCESS, "查询成功", IFrameRoleCategoryMapper.INSTANCE.toVO(frameRoleCategory));
        }
        return new ResultVO<>(Status.SUCCESS, "查询失败");
    }

    @Operation(summary = "查询列表")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<RoleCategoryVO, FrameRoleCategory, RoleCategorySearchDTO>> search(@RequestBody @Validated(IGroup.search.class) RoleCategorySearchDTO dto) {

        TableModel<RoleCategoryVO, FrameRoleCategory, RoleCategorySearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<RoleCategoryVO> fetch(int current, int pageSize) {
                Page<FrameRoleCategory> page = iFrameRoleCategoryService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(IFrameRoleCategoryMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }

    @Operation(summary = "查询角色分类树")
    @PostMapping(value = "/fetchTree")
    public ResultVO<TreeModel<RoleCategoryVO>> fetchTree() {
        TreeModel<RoleCategoryVO> treeModel = new TreeModel<>() {
            @Override
            public List<TreeNode<RoleCategoryVO>> fetch() {
                List<TreeNode<RoleCategoryVO>> treeNodeList = new ArrayList<>();
                List<FrameRoleCategory> frameRoleCategoryList = iFrameRoleCategoryService.findList(new QueryWrapper<>());
                for (FrameRoleCategory frameRoleCategory : frameRoleCategoryList) {
                    TreeNode<RoleCategoryVO> node = new TreeNode<>();
                    node.setId(frameRoleCategory.getId());
                    node.setText(frameRoleCategory.getCategory_name());
                    node.setPid(TreeUtil.defaultPid);
                    treeNodeList.add(node);
                }

                return treeNodeList;
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", treeModel);
    }
}
